var express = require('express');
var router = express.Router();


router.post('/basicFile', function(req, res, next) {  //基础信息
  authentication(req, res);
  if (!req.files){
    return res.status(400).send({back:"没有文件上传"});
  }
  let file = req.files.file;
  let time = new Date()
  file.mv('../documents/'+'('+time+')'+file.name, function(err) {
    if (err)
      return res.status(500).send(err);
 
    res.send('File uploaded!');
  });
});

router.post('/cashFile', function(req, res, next) {  //现金流信息
  authentication(req, res);
  if (!req.files)
    return res.status(400).send('No files were uploaded.');
 
  // The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file 
  let sampleFile = req.files.sampleFile;
 
  // Use the mv() method to place the file somewhere on your server 
  sampleFile.mv('/somewhere/on/your/server/filename.jpg', function(err) {
    if (err)
      return res.status(500).send(err);
 
    res.send('File uploaded!');
  });
});

router.delete('/delete/:filename',function(req,res,next){
  console.log(req.body.params.filename)
  res.send("test successful")
});


function authentication(req, res) {
    if (!req.session.user) {
    	req.session.error="please login"
        return res.redirect('/login');
    }
};

module.exports = router;